package org.example.fastproject.web.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.example.fastproject.core.bean.PageParam;
import org.example.fastproject.core.bean.ViewResult;
import org.example.fastproject.web.sys.model.SysMenu;
import org.example.fastproject.web.sys.model.dto.menu.MenuInsertDto;
import org.example.fastproject.web.sys.model.dto.menu.MenuUpdateDto;
import org.example.fastproject.web.sys.model.vo.menu.MenuNode;
import org.example.fastproject.web.sys.model.vo.menu.MenuVo;
import org.example.fastproject.web.sys.service.SysMenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 王海舟
 * @since 2023-01-31 10:50:22
 */
@RestController
@RequestMapping("/sys/sysMenu")
@Api(tags = "系统：菜单管理")
public class SysMenuController {
    @Resource
    private SysMenuService menuService;

    @GetMapping("/tree")
    @ApiOperation("获取菜单树")
    public ViewResult<List<MenuNode>> tree(){
        List<MenuNode> tree = menuService.listTree();
        return ViewResult.success(tree);
    }

    @PostMapping("/save")
    @ApiOperation("新增菜单")
    @PreAuthorize("hasAuthority('menu.save')")
    public ViewResult<Long> save(@RequestBody MenuInsertDto dto){
        boolean success = menuService.save(dto);
        if(success)
            return ViewResult.success("菜单保存成功");
        return ViewResult.error("菜单保存失败");
    }

    @GetMapping("/remove")
    @ApiOperation("删除菜单")
    @PreAuthorize("hasAuthority('menu.remove')")
    public ViewResult<Long> remove(@RequestParam Long id){
        boolean success = menuService.remove(id);
        if(success)
            return ViewResult.success("菜单删除成功");
        return ViewResult.error("菜单删除失败");
    }

    @PostMapping("/update")
    @ApiOperation("更新菜单信息")
    @PreAuthorize("hasAuthority('menu.update')")
    public ViewResult update(@RequestBody MenuUpdateDto dto){
        boolean success = menuService.update(dto);
        if(success)
            return ViewResult.success("菜单更新成功");
        return ViewResult.error("菜单更新失败");
    }

    @PostMapping("/page")
    @ApiOperation("分页获取菜单信息列表")
    @PreAuthorize("hasAuthority('menu.page')")
    public ViewResult<Page<MenuVo>> page(@RequestBody PageParam param){
        Page<MenuVo> page = menuService.page(param);
        return ViewResult.success(page);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过菜单id获取菜单信息")
    @PreAuthorize("hasAuthority('menu.id')")
    public ViewResult<SysMenu> getById(@PathVariable Long id){
        SysMenu menu = menuService.getById(id);
        return ViewResult.success(menu);
    }
}
